home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / elv18src.zip / main.c < prev    next >
C/C++ Source or Header  |  1994-01-16  |  12KB  |  595 lines

  1. /* main.c */
  2.  
  3. /* Author:
  4.  *    Steve Kirkendall
  5.  *    14407 SW Teal Blvd. #C
  6.  *    Beaverton, OR 97005
  7.  *    kirkenda@cs.pdx.edu
  8.  */
  9.  
  10.  
  11. /* This file contains the main() function of vi */
  12.  
  13. /* HACK! bcc needs to disable use of precompiled headers for this file,
  14.    or else command line args will not be passed to elvis */
  15. #if __BORLANDC__
  16. #include "borland.h"
  17. #endif
  18.  
  19. #include "config.h"
  20. #include <setjmp.h>
  21. #include "vi.h"
  22.  
  23. extern SIGTYPE    trapint(); /* defined below */
  24. jmp_buf        jmpenv;
  25.  
  26. #ifndef NO_DIGRAPH
  27. static init_digraphs P_((void));
  28. #endif
  29.  
  30. /*---------------------------------------------------------------------*/
  31.  
  32. #ifndef NO_GNU
  33. /* This function writes a string out to stderr */
  34. static void errout(str)
  35.     char    *str;    /* the string to be output */
  36. {
  37.     write(2, str, strlen(str));
  38. }
  39. #endif
  40.  
  41. /*---------------------------------------------------------------------*/
  42.  
  43. #if AMIGA
  44. # include "amiwild.c"
  45. main (argc, argv)
  46. #else
  47. # if VMS
  48. #  include "vmswild.c"
  49. main (argc, argv)
  50. # else
  51. #  if TURBOC
  52. int main(argc, argv)
  53. #  else
  54. void main(argc, argv)
  55. #  endif
  56. # endif
  57. #endif
  58.     int    argc;
  59.     char    *argv[];
  60. {
  61.     int    i;
  62.     char    *cmd = (char *)0;
  63.     char    *err = (char *)0;
  64.     char    *str;
  65.     char    *tag = (char *)0;
  66.  
  67. #ifdef __EMX__
  68.     /* expand wildcards a la Unix */
  69.     _wildcard(&argc, &argv);
  70. #endif
  71. #ifndef NO_GNU
  72.     /* check for special GNU options. */
  73.     if (argc == 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-?")))
  74.     {
  75.         errout("usage: ");
  76.         errout(argv[0]);
  77.         errout(" [options] [+excmd] [files]...\n");
  78.         errout("options: -R          readonly -- discourage accidental overwrites\n");
  79.         errout("         -v          start in \"vi\" screen editor mode\n");
  80.         errout("         -e          start in \"ex\" line editor mode\n");
  81. # ifndef NO_EXTENSIONS
  82.         errout("         -i          start in vi's \"input\" mode\n");
  83. # endif
  84.         errout("         -t tagname  begin by looking up the tag \"tagname\"\n");
  85. # ifndef NO_ERRLIST
  86.         errout("         -m errlist  begin at first error listed in file \"errlist\"\n");
  87. # endif
  88. # ifndef CRUNCH
  89.         errout("         -c excmd    begin by executing ex command \"excmd\"\n");
  90.         errout("         -w wsize    set window size to \"wsize\" lines\n");
  91. # endif
  92.         exit(0);
  93.     }
  94.     else if (argc == 2 && !strcmp(argv[1], "--version"))
  95.     {
  96.         errout(VERSION);
  97.         errout("\n");
  98.         exit(0);
  99.     }
  100. #endif /* ndef NO_GNU*/
  101.  
  102.     /* set mode to MODE_VI or MODE_EX depending on program name */
  103.     switch (argv[0][strlen(argv[0]) - 1])
  104.     {
  105.       case 'x':            /* "ex" */
  106.         mode = MODE_EX;
  107.         break;
  108.  
  109.       case 'w':            /* "view" */
  110.         mode = MODE_VI;
  111.         *o_readonly = TRUE;
  112.         break;
  113. #ifndef NO_EXTENSIONS
  114.       case 't':            /* "edit" or "input" */
  115.         mode = MODE_VI;
  116.         *o_inputmode = TRUE;
  117.         break;
  118. #endif
  119.       default:            /* "vi" or "elvis" */
  120.         mode = MODE_VI;
  121.     }
  122.  
  123. #ifndef DEBUG
  124. # ifdef    SIGQUIT
  125.     /* normally, we ignore SIGQUIT.  SIGINT is trapped later */
  126.     signal(SIGQUIT, SIG_IGN);
  127. # endif
  128. #endif
  129.  
  130.     /* temporarily ignore SIGINT */
  131.     signal(SIGINT, SIG_IGN);
  132.  
  133.     /* start curses */
  134.     initscr();
  135.     cbreak();
  136.     noecho();
  137.     scrollok(stdscr, TRUE);
  138.  
  139.     /* arrange for deadly signals to be caught */
  140. # ifdef SIGHUP
  141.     signal(SIGHUP, deathtrap);
  142. # endif
  143. # ifndef DEBUG
  144. #  ifdef SIGILL
  145.     signal(SIGILL, deathtrap);
  146. #  endif
  147. #  ifdef SIGBUS
  148.     signal(SIGBUS, deathtrap);
  149. #  endif
  150. #  ifdef SIGSEGV
  151.     signal(SIGSEGV, deathtrap);
  152. #  endif
  153. #  ifdef SIGSYS
  154.     signal(SIGSYS, deathtrap);
  155. #  endif
  156. # endif /* !DEBUG */
  157. # ifdef SIGPIPE
  158.     signal(SIGPIPE, deathtrap);
  159. # endif
  160. # ifdef SIGTERM
  161.     signal(SIGTERM, deathtrap);
  162. # endif
  163. # ifdef SIGUSR1
  164.     signal(SIGUSR1, deathtrap);
  165. # endif
  166. # ifdef SIGUSR2
  167.     signal(SIGUSR2, deathtrap);
  168. # endif
  169.  
  170.     /* initialize the options - must be done after initscr(), so that
  171.      * we can alter LINES and COLS if necessary.
  172.      */
  173.     initopts();
  174.  
  175.     /* map the arrow keys.  The KU,KD,KL,and KR variables correspond to
  176.      * the :ku=: (etc.) termcap capabilities.  The variables are defined
  177.      * as part of the curses package.
  178.      */
  179.     if (has_KU) mapkey(has_KU, "k",    WHEN_VICMD|WHEN_INMV, "<Up>");
  180.     if (has_KD) mapkey(has_KD, "j",    WHEN_VICMD|WHEN_INMV, "<Down>");
  181.     if (has_KL) mapkey(has_KL, "h",    WHEN_VICMD|WHEN_INMV, "<Left>");
  182.     if (has_KR) mapkey(has_KR, "l",    WHEN_VICMD|WHEN_INMV, "<Right>");
  183.     if (has_HM) mapkey(has_HM, "^",    WHEN_VICMD|WHEN_INMV, "<Home>");
  184.     if (has_EN) mapkey(has_EN, "$",    WHEN_VICMD|WHEN_INMV, "<End>");
  185.     if (has_PU) mapkey(has_PU, "\002", WHEN_VICMD|WHEN_INMV, "<PageUp>");
  186.     if (has_PD) mapkey(has_PD, "\006", WHEN_VICMD|WHEN_INMV, "<PageDn>");
  187.     if (has_KI) mapkey(has_KI, "i",    WHEN_VICMD|WHEN_INMV, "<Insert>");
  188. #if MSDOS || OS2
  189. # ifdef RAINBOW
  190.     if (!strcmp("rainbow", o_term))
  191.     {
  192.         mapkey("\033[1~",  "/",        WHEN_VICMD,        "<Find>");
  193.         mapkey("\033[3~",  "x",        WHEN_VICMD|WHEN_INMV,    "<Remove>");
  194.         mapkey("\033[4~",  "v",        WHEN_VICMD|WHEN_INMV,    "<Select>");
  195.         mapkey("\033[17~", ":sh\n",    WHEN_VICMD,        "<Intrpt>");
  196.         mapkey("\033[19~", ":q\n",    WHEN_VICMD,        "<Cancel>");
  197.         mapkey("\033[21~", "ZZ",    WHEN_VICMD,        "<Exit>");
  198.         mapkey("\033[26~", "V",        WHEN_VICMD|WHEN_INMV,    "<AddlOp>");
  199.         mapkey("\033[28~", "\\",    WHEN_VICMD|WHEN_INMV,    "<Help>");
  200.         mapkey("\033[29~", "K",        WHEN_VICMD|WHEN_INMV,    "<Do>");
  201.     }
  202.     else
  203. # endif /* RAINBOW */
  204.     {
  205.         mapkey("#s", "B", WHEN_VICMD|WHEN_INMV,    "^<Left>");
  206.         mapkey("#t", "W", WHEN_VICMD|WHEN_INMV,    "^<Right>");
  207.     }
  208. #else /* not MSDOS */
  209. # if AMIGA
  210.     mapkey("\233?~", "\\",    WHEN_VICMD|WHEN_INMV,    "<Help>");
  211. # endif
  212.     if (has_kD && *has_kD != ERASEKEY)
  213.     {
  214.         mapkey(has_kD, "x", WHEN_VICMD|WHEN_INMV, "<Del>");
  215.     }
  216.     else if (ERASEKEY != '\177')
  217.     {
  218.         mapkey("\177", "x", WHEN_VICMD|WHEN_INMV, "<Del>");
  219.     }
  220. #endif
  221.  
  222. #ifndef NO_DIGRAPH
  223.     init_digraphs();
  224. #endif /* NO_DIGRAPH */
  225.  
  226.     /* process any flags */
  227.     for (i = 1; i < argc && *argv[i] == '-'; i++)
  228.     {
  229.         switch (argv[i][1])
  230.         {
  231.           case 'R':    /* readonly */
  232.             *o_readonly = TRUE;
  233.             break;
  234.  
  235.           case 'L':
  236.           case 'r':    /* recover */
  237.             msg("Use the `elvrec` program to recover lost files");
  238.             endmsgs();
  239.             refresh();
  240.             endwin();
  241.             exit(1);
  242.             break;
  243.  
  244.           case 't':    /* tag */
  245.             if (argv[i][2])
  246.             {
  247.                 tag = argv[i] + 2;
  248.             }
  249.             else
  250.             {
  251.                 tag = argv[++i];
  252.             }
  253.             break;
  254.  
  255.           case 'v':    /* vi mode */
  256.             mode = MODE_VI;
  257.             break;
  258.  
  259.           case 'e':    /* ex mode */
  260.             mode = MODE_EX;
  261.             break;
  262. #ifndef NO_EXTENSIONS
  263.           case 'i':    /* input mode */
  264.             *o_inputmode = TRUE;
  265.             break;
  266. #endif
  267. #ifndef NO_ERRLIST
  268.           case 'm':    /* use "errlist" as the errlist */
  269.             if (argv[i][2])
  270.             {
  271.                 err = argv[i] + 2;
  272.             }
  273.             else if (i + 1 < argc)
  274.             {
  275.                 err = argv[++i];
  276.             }
  277.             else
  278.             {
  279.                 err = "";
  280.             }
  281.             break;
  282. #endif
  283. #ifndef CRUNCH
  284.          case 'c':    /* run the following command, later */
  285.             if (argv[i][2])
  286.             {
  287.                 cmd = argv[i] + 2;
  288.             }
  289.             else
  290.             {
  291.                 cmd = argv[++i];
  292.             }
  293.             break;
  294.  
  295.           case 'w':    /* set the window size */
  296.             if (argv[i][2])
  297.             {
  298.                 *o_window = atoi(argv[i] + 2);
  299.             }
  300.             else
  301.             {
  302.                 *o_window = atoi(argv[++i]);
  303.             }
  304.             break;
  305. #endif
  306.           default:
  307.             msg("Ignoring unknown flag \"%s\"", argv[i]);
  308.         }
  309.     }
  310.  
  311.     /* if we were given an initial ex command, save it... */
  312.     if (i < argc && *argv[i] == '+')
  313.     {
  314.         if (argv[i][1])
  315.         {
  316.             cmd = argv[i++] + 1;
  317.         }
  318.         else
  319.         {
  320.             cmd = "$"; /* "vi + file" means start at EOF */
  321.             i++;
  322.         }
  323.     }
  324.  
  325.     /* the remaining args are file names. */
  326.     if (i < argc)
  327.     {
  328.         unsigned char *p = args - 1;
  329.  
  330.         strcpy(args, argv[i]);
  331.         /* convert spaces in filenames to CRs so can use space as
  332.          * delimiter for multiple filenames */
  333.         while (*++p)
  334.         {
  335.             if (*p == ' ')
  336.             {
  337.                 *p = SPACEHOLDER;
  338.             }
  339.         }
  340.         while (++i < argc && strlen(args) + 1 + strlen(argv[i]) < sizeof args)
  341.         {
  342.             *p = ' ';
  343.             strcpy(p+1, argv[i]);
  344.             while (*++p)
  345.             {
  346.                 if (*p == ' ')
  347.                 {
  348.                     *p = SPACEHOLDER;
  349.                 }
  350.             }
  351.         }
  352. #ifndef __EMX__
  353. # if MSDOS || TOS || OS2
  354.         /* expand wildcard characters, if necessary */
  355.         if (strchr(args, '*') || strchr(args, '?'))
  356.         {
  357.             strcpy(args, wildcard(args));
  358.         }
  359. # endif
  360. #endif
  361.         strcpy(tmpblk.c, args);
  362.         cmd_args(MARK_UNSET, MARK_UNSET, CMD_ARGS, TRUE, tmpblk.c);
  363.     }
  364.     else
  365.     {
  366.         /* empty args list */
  367.         args[0] = '\0';
  368.         nargs = 1;
  369.         argno = -1;
  370.     }
  371.  
  372.     /* perform the .exrc files and EXINIT environment variable */
  373. #ifdef SYSEXRC
  374.     doexrc(SYSEXRC);
  375. #endif
  376. #ifdef EXINIT
  377.     str = getenv(EXINIT);
  378.     if (str)
  379.     {
  380.           if (*str == '"')
  381.               str++;
  382.         if (str[strlen(str) - 1] == '"')
  383.               str[strlen(str) - 1]